/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.blank.white.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.blank.black.entity.bo.example.CommonExample;
import com.blank.black.enums.LogicEnum;
import com.blank.black.mapper.CommonMapper;
import com.blank.black.service.IBlankConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Controller
public class BasicController {
    @Resource
    CommonMapper commonMapper;
    @Resource
    private IBlankConfigService blankConfigService;


    // http://127.0.0.1:8080/hello?name=lisi
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        return "Hello " + name;
    }

    // http://127.0.0.1:8080/user
    @RequestMapping("/user")
    @ResponseBody
    public User user() {
        User user = new User();
        user.setName("theonefx");
        user.setAge(666);
        return user;
    }

    // http://127.0.0.1:8080/save_user?name=newName&age=11
    @RequestMapping("/save_user")
    @ResponseBody
    public String saveUser(User u) {
        return "user will save: name=" + u.getName() + ", age=" + u.getAge();
    }

    // http://127.0.0.1:8080/html
    @RequestMapping("/html")
    public String html() {
        return "index.html";
    }

    @ModelAttribute
    public void parseUser(@RequestParam(name = "name", defaultValue = "unknown user") String name
            , @RequestParam(name = "age", defaultValue = "12") Integer age, User user) {
        user.setName("zhangsan");
        user.setAge(18);
    }


    @RequestMapping("/test")
    @ResponseBody
    public String test(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        CommonExample example = new CommonExample();

        List<String> colList = new ArrayList<>();
        colList.add("goodsid");
        colList.add("goodsname");
        example.setTableName("goods", "t1");
        example.setSeparator(LogicEnum.OR);
        example.createCriteria()
                .equalTo(LogicEnum.AND, "goodsid", "1")
//                .notEqualTo(LogicEnum.AND,"goodsid","2")
                .like("t1", LogicEnum.OR, "goodsname", "山东")
        ;
        example.addJoin("goodstype")
                .addRelation("t1", "goodstype", "goodstype", "Typeid");
        example.addCol(colList)
                .addCol("t1", "price", "ps")
                .addCol("goodstype", "typename", "tName");
        ;
//        CommonExample.Criteria criteria = example.nextGroup();
//        criteria.equalTo(LogicEnum.AND,"goodsid","1")
//                .notEqualTo(LogicEnum.AND,"goodsid","3");
        long countByExample = commonMapper.countByExample(example);
        List<Map<String, Object>> maps = commonMapper.selectByExample(example);
        example.clear();
        return maps.toString();
    }

    @RequestMapping("/test2")
    @ResponseBody
    public Map<String, Object> test(@RequestParam(name = "id", defaultValue = "0") Long id, @RequestBody JSONObject jsonObject) throws Exception {
        return blankConfigService.listByConfigId(id,jsonObject.getInnerMap());
    }

    @RequestMapping("/test3/{configName}")
    @ResponseBody
    public Map<String, Object> test3(@PathVariable("configName")String configName, @RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String requestURI = request.getRequestURI();
        // requestURI 按 / 分割
        if (StringUtils.isEmpty(configName)) {
            throw new Exception("参数异常");
        }
        List<String> list = Arrays.asList(requestURI.split("/"));
        if (list.indexOf(configName)>0){
            return blankConfigService.listByConfigName(list.get(list.indexOf(configName) - 1),configName,jsonObject.getInnerMap());
        }else {
            throw new Exception("请重新配置Controller");
        }

    }

    /**
     * 根据配置ID 插入业务数据
     * @param id
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Integer insert(@RequestParam(name = "id", defaultValue = "0") Long id, @RequestBody JSONObject jsonObject) throws Exception {
        return blankConfigService.insertByConfigId(id,jsonObject.getInnerMap());
    }

    /**
     * 根据配置ID 更新业务数据
     * @param id
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @RequestMapping("/update")
    @ResponseBody
    public Integer update(@RequestParam(name = "id", defaultValue = "0") Long id, @RequestBody JSONObject jsonObject) throws Exception {
        return blankConfigService.updateByConfigId(id,jsonObject.getInnerMap());
    }

    /**
     * 根据配置ID 删除业务数据
     * @param id
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(@RequestParam(name = "id", defaultValue = "0") Long id, @RequestBody JSONObject jsonObject) throws Exception {
        return blankConfigService.deleteByConfigId(id,jsonObject.getInnerMap());
    }


}
